package org.keyyond.ms.commonservice.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.keyyond.ms.commonservice.common.ResultInfo;
import org.keyyond.ms.commonservice.model.SDBDUser;
import org.keyyond.ms.commonservice.service.SDBDUserService;
import org.keyyond.ms.commonservice.util.CommonBeanUtils;
import org.keyyond.ms.core.model.MsCommonMessageLogVo;
import org.keyyond.ms.core.util.CommonBeanUtil;
import org.keyyond.ms.core.util.Constant;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("/sdbdUser")
@RefreshScope
public class SDBDUserController {
	
	private static Logger logger = Logger.getLogger(SDBDUserController.class);
	
	@Autowired
	private CommonBeanUtils beanUtils;
	
	@Autowired
	private SDBDUserService userService;
	
	@RequestMapping(value = "/selectByUsernameAndPassword", method = RequestMethod.POST)
	public String selectByUsernameAndPassword(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				SDBDUser user = JsonUtil.readJson2Bean(paramArray.get(0).toString(), SDBDUser.class);
				user = userService.selectByUsernameAndPassword(user);
				dataArray.add(JsonUtil.beanToJson(user));
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/getAllUsersCount", method = RequestMethod.POST)
	public String getAllUsersCount(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				SDBDUser user = (SDBDUser) JsonUtil.jsonTBean(paramArray.get(0).toString(), SDBDUser.class);
				int count = userService.getAllUsersCount(user);
				dataArray = JSONArray.fromObject(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public String addUser(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				SDBDUser user = (SDBDUser) JsonUtil.jsonTBean(paramArray.get(0).toString(), SDBDUser.class);
				int count = userService.addUser(user);
				dataArray = JSONArray.fromObject(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/getUserCountByUsername", method = RequestMethod.POST)
	public String getUserCountByUsername(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				String userName =paramArray.get(0).toString();
				int count = userService.getUserCountByUsername(userName);
				dataArray = JSONArray.fromObject(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/deleteUserBySeqno", method = RequestMethod.POST)
	public String deleteUserBySeqno(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				String seqno =paramArray.get(0).toString();
				int count = userService.deleteUserBySeqno(seqno);
				dataArray = JSONArray.fromObject(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/selectAllByPage", method = RequestMethod.POST)
	public String selectAllByPage(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				JSONObject dataJson = new JSONObject();
				dataJson = JSONObject.fromObject(paramArray.get(0));
				Page<SDBDUser> userPage = userService.selectAllByPage(dataJson);
				ResultInfo<SDBDUser> userInfo = new ResultInfo<SDBDUser>(userPage);
				JSONArray userArr = new JSONArray();
				for(SDBDUser u : userInfo.getList()) {
					userArr.add(JsonUtil.beanToJson(u));
				}
				JSONObject resJson = new JSONObject();
				resJson.put("user", userArr);
				resJson.put(Constant.PAGE_TOTALCOUNT, userInfo.getTotalItemCount());
				dataArray.add(resJson);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}

	@RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.POST)
	public String selectByPrimaryKey(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				SDBDUser user = (SDBDUser) JsonUtil.jsonTBean(paramArray.get(0).toString(), SDBDUser.class);
				user = userService.selectByPrimaryKey(user.getSeqno());
				dataArray = JSONArray.fromObject(user);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
}
